import { isMobile } from './is';
// 密码必须为6-18位字母、数字
const passwordValid: any = (rule: any, value: any, callback: any) => {
  if (!/^(?![^a-zA-Z]+$)(?!\D+$)/.test(value)) {
    callback(new Error('6-18位字母、数字'));
  } else {
    callback();
  }
};
// 大于0的整数
const upZeroInt = (rule: any, value: any, callback: any) => {
  if (!/^\+?[1-9]\d*$/.test(value)) {
    callback(new Error('大于0的整数'));
  } else {
    callback();
  }
};
const upZeroIntCanNull = (rule: any, value: any, callback: any) => {
  if (!value) {
    callback();
  } else {
    if (!/^\+?[1-9]\d*$/.test(value)) {
      callback(new Error('大于0的整数'));
    } else {
      callback();
    }
  }
};
const validatePass = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'));
  } else {
    callback();
  }
};

export const formRules = {
  isRequired: (name) => ({
    required: true,
    message: `${name}不能为空`,
    trigger: 'blur'
  }),
  mobileRequire: {
    required: true,
    message: '手机号码不能为空',
    trigger: 'blur'
  },
  mobileValid: {
    required: false,
    message: '手机号码有误',
    validator: (rule, value) => isMobile(value),
    trigger: 'blur'
  },
  isNotNull: [{ required: true, message: '该字段不能为空', trigger: 'blur' }],
  isNotNullSecond: [{ required: true, message: '不能为空', trigger: 'blur' }],
  mLength8: [
    { required: true, message: '该字段不能为空', trigger: 'blur' },
    { max: 8, message: '最长为8个字符', trigger: 'blur' }
  ],
  minLength7: [
    { required: true, message: '该字段不能为空', trigger: 'blur' },
    { min: 7, message: '最小7个字符', trigger: 'blur' }
  ],
  length17: [
    { required: true, message: '该字段不能为空', trigger: 'blur' },
    { min: 17, max: 17, message: '长度为17个字符', trigger: 'blur' }
  ],
  desc: [{ validator: validatePass, trigger: 'blur' }],
  upZeroInt: [{ validator: upZeroInt, trigger: 'blur' }],
  upZeroIntCanNull: [{ validator: upZeroIntCanNull, trigger: 'blur' }],
  passwordValid: [{ validator: passwordValid, trigger: 'blur' }]
};
